<?php
namespace Home\Controller;

/**
 * AdvController
 * 广告
 */
class AdvController extends CommonController {
    /**
     * 广告列表
     * @return
     */
    public function index() {
        $result = $this->getPagination('Ad',['is_del'=>0]);

        $this->assign('list', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display("Ad/index");
    }

    /**
     * 添加广告页面
     */
    public function add(){
        $this->display("Ad/add");
    }

    /**
     * 添加广告操作
     */
    public function insert(){
        $Ad_Model = M('ad');
        $data = I('post.data');
        if (!isset($data)) {
            return $this->errorReturn('无效的操作！');
        }

        if($data['title'] == '' || !$data['title']){
            return $this->errorReturn('广告名称不能为空！');
        }elseif($data['url'] == '' || !$data['url']){
            return $this->errorReturn('广告链接不能为空！');
        }

        //上传广告图片
        if($_FILES['pic']['tmp_name']){
            $info = $this->imgUpload('ad_pic');
            if(!$info){
                return $this->errorReturn('广告图片上传失败请重试！');
            }else{
                $data['pic'] = '/Public/uploads/'.$info['pic']['savepath'].$info['pic']['savename'];
            }
        }

        $data['create_time'] = time();
        $data['update_time'] = time();
        $result = $Ad_Model->add($data);
        if (!$result) {
            return $this->errorReturn('添加失败，稍后请重试！');
        }

        return $this->successReturn('添加广告成功！', U('Adv/index'));
    }


    /**
     * 编辑广告页面
     * @return
     */
    public function edit() {
        if (!isset($_GET['id'])) {
            return $this->error('参数错误，稍后请重试！');
        }

        //广告详情
        $Novel = M('ad')->getById($_GET['id']);

        $this->assign('ad', $Novel);

        $this->assign('id',$_GET['id']);
        $this->display("Ad/edit");
    }

    /**
     * 更新广告信息
     * @return
     */
    public function update() {
        $Ad_Model = M('ad');
        $data = I('post.data');
        if(!isset($data)){
            return $this->errorReturn('无效的操作！');
        }

        if($data['title'] == '' || !$data['title']){
            return $this->errorReturn('广告名称不能为空！');
        }elseif($data['url'] == '' || !$data['url']){
            return $this->errorReturn('广告链接不能为空！');
        }

        $data['update_time'] = time();

        //上传封面
        if($_FILES['pic']['tmp_name']){
            $info = $this->imgUpload('ad_pic');
            if(!$info){
                return $this->errorReturn('封面上传失败请重试！');
            }else{
                $data['pic'] = '/Public/uploads/'.$info['pic']['savepath'].$info['pic']['savename'];
            }
        }

        $result = $Ad_Model->where(['id'=>$data['id']])->save($data);
        if (!$result) {
            return $this->errorReturn('操作失败，请重试！');
        }

        return $this->successReturn('更新广告信息成功！', U('Adv/index'));
    }

    /**
     * 删除广告
     */
    public function del(){
        $ad_Model = M('ad');
        $id = I('post.id');
        if(!$id){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数错误，请重试']);
        }

        $result = $ad_Model->where(['id'=>$id])->setField('is_del',1);
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'删除成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'删除失败，请重试!']);
        }
    }


}
